﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using App.Core.Extensions;
using App.Data;
using System.Text;

public partial class Pages_Public_EventDetails : BasePage
{
    protected int _EventID;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            BindEventDetails();
        }
    }
    protected void BindPageInfo()
    {
        _EventID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.ID);
        Page.Title = WebUtil.GetPageTitle("Event Details");
    }
    protected void BindEventDetails()
    {
        BtcHousingEntities context = new BtcHousingEntities();
        var entity = context.Events.FirstOrDefault(P => P.ID == _EventID);
        if (entity == null)
            ShowNotFoundMessage();
        else
        {
            divTitle.InnerHtml = entity.Title.HtmlEncode();
            divDate.InnerHtml = GetDateAndVenue(entity); 
            divDescription.InnerHtml = WebUtil.FormatText(entity.Description) ;

            if (!entity.ImageFile.IsNullOrEmpty())
            {
                EventImageFile.InnerHtml = String.Format("<img class='EventImage' src='{0}/{1}' alt='' title='' />", AppConstants.EVENT_FILES, entity.ImageFile);
                EventImageFile.Visible = true;
            }
        }
    }

    private string GetDateAndVenue(Event entity)
    {
        StringBuilder sb = new StringBuilder(10);        
        if (!entity.Venue.IsNullOrEmpty())
            sb.AppendFormat("Venue: {0} &nbsp;&nbsp;", entity.Venue.HtmlEncode());
        if (!entity.EventDate.IsNullOrEmpty())
            sb.AppendFormat("On {0}", entity.EventDate.HtmlEncode());
        return sb.ToString();
    }
    protected void ShowNotFoundMessage()
    {
        WebUtil.ShowMessageBox(divMessage, "Sorry! Requested Event Was not found.", true);
        pnlFormContainer.Visible = false;
    }

    [WebMethod]
    public static int SaveEventBooking(App.CustomEntity.EventBooking customEventBooking)
    {
        //System.Threading.Thread.Sleep(5000);
        BtcHousingEntities context = new BtcHousingEntities();
        if (context.EventBookings.FirstOrDefault(E => E.EventID == customEventBooking.EventID && String.Compare(E.Email, customEventBooking.Email, true) == 0) == null)
        {
            EventBooking booking = new EventBooking();
            booking.EventID = customEventBooking.EventID;
            booking.Email = customEventBooking.Email;
            booking.Address = customEventBooking.Address;
            booking.ContactNo = customEventBooking.ContactNo;
            booking.Name = customEventBooking.Name;
            booking.Created = DateTime.Now;
            booking.Modified = DateTime.Now;

            context.AddToEventBookings(booking);
            context.SaveChanges();
            return 1;
        }
        else
            return -1;
    }
}